using System;
using System.Configuration;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Catalyst.Caching
{
	public class SocketPoolElementCollection : ConfigurationElementCollection
	{
		public SocketPoolElement this[int index]
		{
			get
			{
				return (SocketPoolElement)BaseGet(index);
			}
			set
			{
				if (BaseGet(index) != null)
				{
					BaseRemoveAt(index);
				}

				BaseAdd(index, value);
			}
		}

		protected override ConfigurationElement CreateNewElement()
		{
			return new SocketPoolElement();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((SocketPoolElement)element).Name;
		}
	}

	[Serializable]
	public class SocketPoolElement : ConfigurationElement
	{
		[ConfigurationProperty("servers")]
		[ConfigurationCollection(typeof(ServerElementCollection),
			AddItemName = "add",
			ClearItemsName = "clear",
			RemoveItemName = "remove")]
		public ServerElementCollection Servers
		{
			get { return (ServerElementCollection)this["servers"]; }
		}

		[ConfigurationProperty("name")]
		public string Name
		{
			get { return this["name"].ToString(); }
		}

		[ConfigurationProperty("userName")]
		public string UserName
		{
			get { return this["userName"].ToString(); }
		}

		[ConfigurationProperty("password")]
		public string Password
		{
			get { return this["password"].ToString(); }
		}

		[ConfigurationProperty("minPoolSize", DefaultValue = 10)]
		public int MinPoolSize
		{
			get { return (int)this["minPoolSize"]; }
		}

		[ConfigurationProperty("maxPoolSize", DefaultValue = 100)]
		public int MaxPoolSize
		{
			get { return (int)this["maxPoolSize"]; }
		}

		[ConfigurationProperty("connectionTimeout", DefaultValue = "00:00:10")]
		public string ConnectionTimeout
		{
			get { return this["connectionTimeout"].ToString(); }
		}

		[ConfigurationProperty("deadTimeout", DefaultValue = "00:02:00")]
		public string DeadTimeout
		{
			get { return this["deadTimeout"].ToString(); }
		}
	}
}